package com.workshop.web;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.commom.ResponseUtils;
import com.base.commom.http.RequestUtil;
import com.base.commom.http.ResponseEntity;
import com.workshop.service.IUUserService;
import com.workshop.utils.ResultMsg;
import com.workshop.utils.dto.UserDto;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengluqiang
 * @since 2019-04-13
 */
@RestController
@RequestMapping("/uUser")
public class UUserController {
	
	@Resource
	private IUUserService userService;
	
	@Value("${wx.getOpenId.api}")
	private String getOpenIdApi;

	@Value("${wx.appid}")
	private String appidWX;

	@Value("${wx.key}")
	private String keyWX;
	
	/**
	 * 微信用户第一次登录注册用户请求接口
	 *
	 * @author lihongwu
	 * 2018年12月14日 上午11:07:45
	 * @param userJson
	 * @param response
	 * @param request
	 * @return
	 */
	@PostMapping("/wechatLogin.wx")
	public Map<String, Object> wechatLogin(
			String userJson,
			HttpServletResponse response,
			HttpServletRequest request){
		//防止重复登录
		Map<String, Object> checkRepeatLogin = this.checkRepeatLogin(request);
		if(checkRepeatLogin!=null) {
			return checkRepeatLogin;
		}
		UserDto userDto = JSON.parseObject(userJson,UserDto.class);
		return userService.registerUserByWechat(userDto, request, response);
	}
	
	
	/**
	 * 防止重复登录
	 *
	 * @author lihongwu
	 * 2018年12月14日 上午11:08:14
	 * @param request
	 * @return
	 */
	private Map<String, Object> checkRepeatLogin(HttpServletRequest request) {
		String key = request.getHeader(ResultMsg.USERTOKEN);
		if(!StringUtils.isNotBlank(key)) {
			return null;
		}
		HttpSession session = request.getSession();
		String val = (String) session.getAttribute(key);
		if(val!=null&&StringUtils.isNotBlank(val)) {
			return ResponseUtils.success("ok",ResultMsg.USERTOKEN,key);
		}
		return null;
	}
	
	/**
	 * 获取openId
	 *
	 * @author lihongwu
	 * 2018年12月14日 上午11:08:18
	 * @param request
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getOpenId.wx")
	public Map<String, Object> getOpenId(HttpServletRequest request,String code) throws Exception{
		if(!StringUtils.isNotBlank(code)) {
			return ResponseUtils.error("系统繁忙,请稍后重试!","code");
		}
		Map<String,String> map = new HashMap<>();
		map.put("appid", appidWX);
		map.put("secret", keyWX);
		map.put("js_code", code);
		map.put("grant_type", "authorization_code");
				
		ResponseEntity httpClientPost = RequestUtil.httpClientGet(getOpenIdApi,map,null);
		
		String content = httpClientPost.getContent();
		
		JSONObject result=JSONObject.parseObject(content);
		
		Object errcode = result.get("errcode");
		if(errcode!=null&&errcode!="") {
			return ResponseUtils.error("error", result);
		}else {
			return ResponseUtils.success("ok", "openId", result.get("openid").toString());
		}
	}
}

